package com.tigercar.mainbody.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tigercar.common.pojo.PageResult;
import com.tigercar.mainbody.DTO.ServiceDTO;
import com.tigercar.mainbody.DTO.ServiceDetailsDTO;
import com.tigercar.mainbody.DTO.SubjectDTO;
import com.tigercar.mainbody.DTO.SubjectDetailsDTO;
import com.tigercar.mainbody.mapper.SubjectDetailsMapper;
import com.tigercar.mainbody.mapper.SubjectMapper;
import com.tigercar.mainbody.pojo.Subject;
import com.tigercar.mainbody.pojo.SubjectDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ruiz
 * @date 2021/4/23 10:07
 */
@Slf4j
@Service
public class MainBodyService {

    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private SubjectDetailsMapper subjectDetailsMapper;

    public PageResult<ServiceDTO> findAll(Integer page, Integer pageSize, String shortName, Long number, String province, String city, String region, String phone, Boolean isChain, Boolean status) {
        IPage<Subject> iPage = new Page<Subject>(page, pageSize);
        Subject subject = new Subject();
        subject.setSubjectCode(number);
        subject.setShortName(shortName);
        subject.setContactsPhone(phone);
        subject.setLockStatus(status);
        subject.setProvince(province);
        subject.setCity(city);
        subject.setRegion(region);
        subject.setSubjectType(1);
        QueryWrapper<Subject> query = Wrappers.query(subject);
        if (isChain != null && isChain) {
            query.isNotNull("chain_store");
        }
        IPage<Subject> subjectIPage = subjectMapper.selectPage(iPage, query);
        PageResult<ServiceDTO> subjectPageResult = new PageResult<>();
        subjectPageResult.setTotal(subjectIPage.getTotal());
        subjectPageResult.setTotalPage(subjectIPage.getPages());
        List<ServiceDTO> list = new ArrayList<>();
        subjectIPage.getRecords().forEach(subject1 -> {
            ServiceDTO serviceDTO = new ServiceDTO();
            serviceDTO.setNumber(subject1.getSubjectCode().toString());
            serviceDTO.setShortName(subject1.getShortName());
            serviceDTO.setContact(subject1.getContacts());
            serviceDTO.setPhone(subject1.getContactsPhone());
            serviceDTO.setIsChian("否");
            if (StringUtils.isNotEmpty(subject1.getChainStore())) {
                serviceDTO.setIsChian("是");
            }
            serviceDTO.setArea(subject1.getCity() + subject1.getRegion());
            serviceDTO.setStatus("正常");
            if (subject1.getLockStatus()) {
                serviceDTO.setStatus("锁定");
            }
            list.add(serviceDTO);
        });
        subjectPageResult.setItems(list);
        return subjectPageResult;
    }

    public PageResult<SubjectDTO> findAllParts(Integer page, Integer pageSize, String shortName, Long number, Boolean status, String contact) {
        Subject subject = new Subject();
        subject.setSubjectCode(number);
        subject.setShortName(shortName);
        subject.setContacts(contact);
        subject.setLockStatus(status);
        subject.setSubjectType(2);
        IPage<Subject> iPage = new Page<>(page, pageSize);
        QueryWrapper<Subject> query = Wrappers.query(subject);
        IPage<Subject> subjectIPage = subjectMapper.selectPage(iPage, query);
        PageResult<SubjectDTO> result = new PageResult<>();
        result.setTotal(subjectIPage.getTotal());
        result.setTotalPage(subjectIPage.getPages());
        ArrayList<SubjectDTO> list = new ArrayList<>();
        subjectIPage.getRecords().forEach(subject1 -> {
            SubjectDTO subjectDTO = new SubjectDTO();
            subjectDTO.setNumber(subject1.getSubjectCode().toString());
            subjectDTO.setShortName(subject1.getShortName());
            subjectDTO.setContact(subject1.getContacts());
            subjectDTO.setPhone(subject1.getContactsPhone());
            subjectDTO.setArea(subject1.getCity() + subject1.getRegion());
            subjectDTO.setStatus("正常");
            if (subject1.getLockStatus()) {
                subjectDTO.setStatus("锁定");
            }
            list.add(subjectDTO);
        });
        result.setItems(list);
        return result;
    }

    public Boolean lockSubject(Boolean flag, Long number) {
        Subject subject = new Subject();
        subject.setLockStatus(false);
        if (flag) {
            subject.setLockStatus(true);
        }
        UpdateWrapper<Subject> update = Wrappers.update();
        update.eq("subject_code", number);
        return subjectMapper.update(subject, update) > 0;
    }

    public SubjectDetailsDTO findPartInfo(Long number) {
        QueryWrapper<Subject> subjectQuery = Wrappers.query();
        subjectQuery.eq("subject_code", number);
        Subject subject = subjectMapper.selectOne(subjectQuery);
        QueryWrapper<SubjectDetails> subjectDetailsQuery = Wrappers.query();
        subjectDetailsQuery.eq("subject_code", number);
        SubjectDetails subjectDetails = subjectDetailsMapper.selectOne(subjectDetailsQuery);
        SubjectDetailsDTO subjectDetailsDTO = new SubjectDetailsDTO();
        subjectDetailsDTO.setFullName(subjectDetails.getFullName());
        subjectDetailsDTO.setShortName(subject.getShortName());
        subjectDetailsDTO.setContact(subject.getContacts());
        subjectDetailsDTO.setPhone(subject.getContactsPhone());
        subjectDetailsDTO.setProvince(subject.getProvince());
        subjectDetailsDTO.setCity(subject.getCity());
        subjectDetailsDTO.setRegion(subject.getRegion());
        subjectDetailsDTO.setAddress(subjectDetails.getAddress());
        String papers = subjectDetails.getPapers();
        if (papers != null) {
            String[] paper = papers.split(",");
            subjectDetailsDTO.setLicense(paper[0]);
            subjectDetailsDTO.setFrontCard(paper[1]);
            subjectDetailsDTO.setBackCard(paper[2]);
        }
        return subjectDetailsDTO;
    }

    public ServiceDetailsDTO findServiceInfo(Long number) {
        QueryWrapper<Subject> subjectQuery = Wrappers.query();
        subjectQuery.eq("subject_code", number);
        Subject subject = subjectMapper.selectOne(subjectQuery);
        QueryWrapper<SubjectDetails> subjectDetailsQuery = Wrappers.query();
        subjectDetailsQuery.eq("subject_code", number);
        SubjectDetails subjectDetails = subjectDetailsMapper.selectOne(subjectDetailsQuery);
        ServiceDetailsDTO serviceDetailsDTO = new ServiceDetailsDTO();
        serviceDetailsDTO.setFullName(subjectDetails.getFullName());
        serviceDetailsDTO.setShortName(subject.getShortName());
        serviceDetailsDTO.setContact(subject.getContacts());
        serviceDetailsDTO.setPhone(subject.getContactsPhone());
        serviceDetailsDTO.setProvince(subject.getProvince());
        serviceDetailsDTO.setCity(subject.getCity());
        serviceDetailsDTO.setRegion(subject.getRegion());
        serviceDetailsDTO.setAddress(subjectDetails.getAddress());
        String papers = subjectDetails.getPapers();
        if (papers != null) {
            String[] paper = papers.split(",");
            serviceDetailsDTO.setLicense(paper[0]);
            serviceDetailsDTO.setFrontCard(paper[1]);
            serviceDetailsDTO.setBackCard(paper[2]);
        }
        serviceDetailsDTO.setIsChain(false);
        if (subject.getChainStore() != null) {
            serviceDetailsDTO.setIsChain(true);
        }
        serviceDetailsDTO.setTypeTag(subjectDetails.getTypeTag());
        serviceDetailsDTO.setLatitude(serviceDetailsDTO.getLatitude());
        serviceDetailsDTO.setLongitude(serviceDetailsDTO.getLongitude());
        return serviceDetailsDTO;
    }
}
